#####################################################
# Replication Archive: Crumbling in the Face of Cost?
# Mader, Neubert, Münchow, Hofmann, Schoen & Gavras
# 01/2024
#####################################################
# Appendix: Regression Output
#####################################################

# Attention: This script produces a number of .doc-files.
# Do not run unless you want to produce these files.

# define which packages are needed 
p_needed <-
  c( "broom", "hrbrthemes", "viridis", "ggh4x", "texreg", "tidyverse",
    "sjPlot", "dotwhisker", "gridExtra", "grid", "extrafont",
    "ggrepel", "hrbrthemes", "ggExtra")

# check which packages are already installed
packages <- rownames(installed.packages())

# check which of the packages you need are not installed 
p_to_install <- p_needed[!(p_needed %in% packages)]

# install missing packages 
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
# load required packages 
sapply(p_needed, require, character.only = TRUE)

###################################################

load("EUP_Costs_ReplicationData.RData")


###################################################

# Experiment 1: Pooled without controls (for regression output)
experiment1_Aggregate_out <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, socialCosts = "SoldiersRisk")) %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment1_Treat1, costExperiment_Treat2 = costExperiment1_Treat2) %>%
  lm(costExperiment1_CrisisManagement ~ costExperiment_Treat1 + costExperiment_Treat2 + QCOUNTRY, data = .)


# Experiment 2: Pooled without controls (for regression output)
experiment2_Aggregate_out <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, socialCosts = "JobCuts")) %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment2_Treat1, costExperiment_Treat2 = costExperiment2_Treat2) %>%
  lm(costExperiment2_Aircraft ~  costExperiment_Treat1 + costExperiment_Treat2 + QCOUNTRY, data = .)

## Export Regression Tables
wordreg(list(experiment1_Aggregate_out), file = "experiment1.doc")
wordreg(list(experiment2_Aggregate_out), file = "experiment2.doc")


#### Experimental results by country

# Experiment 1: Military intervention (for regression output)
countryModel_ex1_out <- dat %>%
  group_by(QCOUNTRY) %>%
  group_map(~ lm(costExperiment1_CrisisManagement ~ costExperiment1_Treat1 +
                   costExperiment1_Treat2,
                 data = .x))

# add country names
names(countryModel_ex1_out) <- unique(dat$QCOUNTRY)


# Experiment 2: Defence procurement (for regression output)
countryModel_ex2_out <- dat %>%
  group_by(QCOUNTRY) %>%
  group_map(~ lm(costExperiment2_Aircraft ~  costExperiment2_Treat1 +
                   costExperiment2_Treat2,
                 data = .x))

# add country names
names(countryModel_ex2_out) <- unique(dat$QCOUNTRY)


## Regression Table Output: Experiment 1
for(i in 1: length(countryModel_ex1_out)){
  wordreg(list(countryModel_ex1_out[[i]]), 
          file = paste0("ex1country", 
                        names(countryModel_ex1_out)[i], ".doc"))
}

## Regression Table Output: Experiment 2
for(i in 1: length(countryModel_ex2_out)){
  wordreg(list(countryModel_ex2_out[[i]]), 
          file = paste0("ex2country", 
                        names(countryModel_ex2_out)[i], ".doc"))
}


### Experimental results: Interaction effects

## Military Intervention: Political Knowledge
knowledge_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * quiz + costExperiment1_Treat2 * quiz + QCOUNTRY, data = .)

# export regression table
wordreg(list(knowledge_ex1), file = "experiment1_polKnowledge.doc")

# Defence Procurement: Political Knowledge
knowledge_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job Cuts` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * quiz + costExperiment2_Treat2 * quiz + QCOUNTRY, data = .)

# export regression table
wordreg(list(knowledge_ex2), file = "experiment2_polKnowledge.doc")


## Military Intervention: Attitudes towards the EU
EUatt_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * attitudes_EU + costExperiment1_Treat2 * attitudes_EU + QCOUNTRY, data = .)

# export regression table
wordreg(list(EUatt_ex1), file = "experiment1_EUattitudes.doc")

## Defence Procurement, Attitudes towards the EU
EUatt_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job losses` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * attitudes_EU + costExperiment2_Treat2 * attitudes_EU + QCOUNTRY, data = .)

# export regression table
wordreg(list(EUatt_ex2), file = "experiment2_EUattitudes.doc")

## Military Intervention: Militant Internationalism
MI_ex1 <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, `Casualties` = "SoldiersRisk"),
         costExperiment1_Treat2 = fct_recode(costExperiment1_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment1_CrisisManagement ~  costExperiment1_Treat1 * militarism + costExperiment1_Treat2 * militarism + QCOUNTRY, data = .)

# export regression table
wordreg(list(MI_ex1), file = "experiment1_MI.doc")

## Defence Procurement: Militant internationalism
MI_ex2 <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, `Job losses` = "JobCuts"),
         costExperiment2_Treat2 = fct_recode(costExperiment2_Treat2, `National Budget` = "CountryCost", `Pooled Budget` = "SharedCost")) %>%
  lm(costExperiment2_Aircraft ~ costExperiment2_Treat1 * militarism + costExperiment2_Treat2 * militarism + QCOUNTRY, data = .)

# export regression table
wordreg(list(MI_ex2), file = "experiment2_MI.doc")


